define(function () {
    'use strict';

    function ctrl($scope, $filter, $ionicScrollDelegate, $timeout, InfoService,CommonService,$ionicActionSheet) {

        $scope.vm = {};
        $scope.vm.pMap = {};
        init();

        $scope.doRefresh = function() {
            init(true);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.$watch("vm.activeTab", function(newValue, oldValue){
            if(newValue) {
                if(newValue!='101'&&newValue!='102'){
                    $scope.vm.showOrder=true;
                }else{
                    $scope.vm.showOrder=false;
                }
                $timeout(function(){
                    $ionicScrollDelegate.resize();
                    $scope.loadMore();
                }, 500);
            }
        });

        $scope.$watch("vm.showType", function(newValue, oldValue){
            if(newValue) {
                $ionicScrollDelegate.$getByHandle('sonScroll').scrollTo(0, 0, true);
            } else {
                if($scope.vm.pMap) {
                    var left = $scope.vm.pMap[$scope.vm.activeTab];
                    if(document.body.clientWidth/2 < left&&left < document.body.clientWidth){
                        $timeout(function(){
                            console.log(3.5*(left-document.body.clientWidth/2));
                            $ionicScrollDelegate.$getByHandle('sonScroll').scrollTo(3.5*(left-document.body.clientWidth/2), 0, true);
                        },100);
                    }else if(document.body.clientWidth <= left ){
                        $timeout(function(){
                            console.log(document.body.clientWidth);
                            $ionicScrollDelegate.$getByHandle('sonScroll').scrollTo(document.body.clientWidth, 0, true);
                        },100);
                    }
                }
            }
        })

        function init(refresh){
            if(refresh) {
                $scope.vm.loadMore = true;
                $scope.loadMore(refresh);
            } else {
                return InfoService.getChannelInfosByParentMark("fund").then(function(data){
                    $scope.channels = data;
                    for(var i = 0; i < $scope.channels.length; i++) {
                        var channel = $scope.channels[i];
                        if(i == 0 && !$scope.vm.activeTab) {
                            $scope.vm.activeTab=channel.id;
                        }
                        channel.page_no = 1;
                        channel.products = new Array();
                    }
                    $scope.vm.loadMore = true;
                    $timeout(function(){
                        for (var i = 0; i < $scope.channels.length; i++) {
                            var channel = $scope.channels[i];
                            var left = document.getElementById(channel.id).offsetLeft;
                            $scope.vm.pMap[channel.id] = left;
                        }
                    }, 100);
                });
            }
        }

        $scope.show = function() {
            var hideSheet= $ionicActionSheet.show({
                cancelOnStateChange:true,
                cssClass:'action_s',
                titleText: "排序",
                buttons: [
                    { text: $scope.vm.price_increase_qry_type == 1 ? "<b>近一月涨幅</b>" : "近一月涨幅" },
                    { text: $scope.vm.price_increase_qry_type == 2 ? "<b>近三月涨幅</b>" : "近三月涨幅" },
                    { text: $scope.vm.price_increase_qry_type == 3 ? "<b>近半年涨幅</b>" : "近半年涨幅" },
                    { text: $scope.vm.price_increase_qry_type == 4 ? "<b>近一年涨幅</b>" : "近一年涨幅" },
                    { text: $scope.vm.price_increase_qry_type == 5 ? "<b>成立以来涨幅</b>" : "成立以来涨幅" }
                ],
                buttonClicked: function(index) {
                    $scope.vm.price_increase_qry_type= index+1;
                    $ionicScrollDelegate.resize();
                    $scope.loadMore(true);
                    return true;
                },
                cancelText: "取消",
                cancel: function() {
                    console.log('执行了取消操作');
                    return true;
                }
            });
            $timeout(function() {
                hideSheet();
            }, 40000);
        };

        $scope.loadMore = function(refresh) {
            for(var i = 0; i < $scope.channels.length; i++) {
                var channel = $scope.channels[i];
                if($scope.vm.activeTab == channel.id) {
                    if(refresh) {
                        channel.page_no = 1;
                        channel.products = new Array();
                    }
                    InfoService.getProductsByChannelId(channel.id, channel.page_no, 1,$scope.vm.price_increase_qry_type).then(
                        function(products) {
                            if(products && products.length > 0) {
                                for(var i=0;i<products.length;i++){
                                    var fund = products[i];
                                    fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                                    fund.url = CommonService.productPageUrl(fund.prod_profit_mode, fund.prod_source, fund.prod_catalog_mark);
                                }
                                channel.products = channel.products.concat(products);
                                channel.page_no ++;
                                $scope.vm.loadMore = true;
                            } else {
                                $scope.vm.loadMore = false;
                            }
                            $scope.$broadcast('scroll.infiniteScrollComplete');
                        }
                    );
                    break;
                }
            }
        };
    }

    ctrl.$inject = ['$scope','$filter','$ionicScrollDelegate','$timeout', 'InfoService','CommonService','$ionicActionSheet'];
    return ctrl;
});